<template>
  <div class="user">
    <div class="allbutton" style="width:80%;border:1px solid white;margin-top:20px">
      <el-button style="display:inline-block;position:absolute" type="primary" @click="addmodel">新增</el-button>
      <el-input style="margin-left:150px;display:inline-block;position:absolute" v-model="search_name"></el-input>
      <el-button style="margin-left:350px;display:inline-block;position:absolute" type="primary" @click="searchUser">搜索</el-button>
    </div>
    <div class="biaoge">
      <el-table
        :data="tableData"
        stripe
        tooltip-effect="dark"
        style="width: 100%"
        height="450"
      >
        <el-table-column type="selection" width="55"></el-table-column>

        <el-table-column
          prop="username"
          label="用户名"
          width="120"
          sortable
        ></el-table-column>
        <el-table-column prop="password" label="密码" width="150"></el-table-column>
        <el-table-column prop="phone" label="手机号" width="150"></el-table-column>
        <el-table-column prop="email" label="邮箱" width="200"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button type="danger" @click="deldata(scope.row)">删除</el-button>
            <el-button type="primary" @click="editmodel(scope.row)">修改</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!-- 新增用户 -->
    <el-dialog
      title="新增用户"
      :visible.sync="dialogVisibleadd"
      :close-on-click-modal="false"
      :show-close="false"
      :modal-append-to-body="false"
    >
      <el-form :inline="true" :model="add" :rules="rules" ref="addruleForm">
        <el-form-item label="用户姓名" prop="username" label-width="100px">
          <el-input v-model="add.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password" label-width="100px">
          <el-input v-model="add.password"></el-input>
        </el-form-item>

        <el-form-item label="手机号" prop="phone" label-width="100px">
          <el-input v-model="add.phone"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email" label-width="100px">
          <el-input v-model="add.email"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="quxiao">取 消</el-button>
        <el-button type="primary" @click="sureadd">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 修改用户 -->
    <el-dialog
      title="修改用户"
      :visible.sync="dialogVisibleedit"
      :close-on-click-modal="false"
      :show-close="false"
      :modal-append-to-body="false"
    >
      <el-form :inline="true" :model="edit" :rules="rules" ref="editruleForm">
        <el-form-item label="用户姓名" prop="username" label-width="100px">
          <el-input v-model="edit.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password" label-width="100px">
          <el-input v-model="edit.password"></el-input>
        </el-form-item>
        <el-form-item label="手机号" prop="phone" label-width="100px">
          <el-input v-model="edit.phone"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="phone" label-width="150px">
          <el-input v-model="edit.email"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="quxiao">取 消</el-button>
        <el-button type="primary" @click="sureedit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      tableData: [],
      add: {
        username: "",
        password: "",
        phone: "",
      },
      search_name: "",
      edit: {userId:0},
      dialogVisibleedit: false,
      dialogVisibleadd: false,
      rules: {
        username: [{ required: true, message: "请输入用户名", trigger: "blur" }],
        phone: [{ required: true, message: "请输入手机号", trigger: "blur" }],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
      },
      
    };
  },
  created() {
    this.getData();
  },
  methods: {
    getData() {
      this.$axios.get(this.$store.state.url+"/student").then((res) => {
        if (res.status === 200) {
          this.tableData = res.data.results;
        }
      });
    },
    //对话框取消
    quxiao() {
      this.dialogVisibleadd = false;
      this.dialogVisibleedit = false;
    },
    //点开新增对话框
    addmodel() {
      this.dialogVisibleadd = true;
      if (this.$refs["addruleForm"]) {
        this.$refs["addruleForm"].resetFields();
      }
    },
    //确认新增
    sureadd() {
      this.$refs["addruleForm"].validate((valid) => {
        if (valid) {
          this.$axios
            .post(this.$store.state.url+"/student/addStudent", this.add)
            .then((res) => {
              if (res.data.status === 200) {
                this.getData();
                this.dialogVisibleadd = false;
                this.$message.success("添加成功");
              }
            });
        } else {
          this.$message.error("添加失败")
          return false;
        }
      });
    },
    //点开修改对话框
    editmodel(edit) {
      this.edit.userId = edit.uid
      console.log(edit);
      if (edit) {
        this.dialogVisibleedit = true;
      } else {
        //提示不让修改
        this.$message.error("请选择一条数据进行修改");
      }
      if (this.$refs["editruleForm"]) {
        this.$refs["editruleForm"].resetFields();
      }
    },
    //确定修改
    sureedit() {
      this.$refs["editruleForm"].validate((valid) => {
        if (valid) {
          this.$axios.post(this.$store.state.url+"/updateStudent", this.edit,this.userId).then((res) => {
            if (res.status === 200) {
              this.getData();
              this.dialogVisibleedit = false;
              this.$message.success("修改成功")
            } else {
              this.$message({
                showClose: false,
                message: res.data.msg,
                type: "error",
              });
            }
          });
        } else {
          this.$message.error("修改失败")
          return false;
        }
      });
    },
    //删除
    deldata(del) {
      if (del) {
        this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
          .then(() => {
            this.$axios
              .get(this.$store.state.url+"/deleteStudent/?uid=" + del.uid)
              .then((res) => {
                if (res.data.status === 200) {
                  this.getData();
                  this.$message({
                    type: "success",
                    message: "删除成功!",
                  });
                } else {
                  this.$message({
                    showClose: false,
                    message: res.data.msg,
                    type: "error",
                  });
                }
              })
          })
          .catch(() => {
            this.$message({
              type: "info",
              message: "已取消删除",
            });
          });
      } else {
        this.$message.error("请选择一条数据进行删除");
      }
    },
    searchUser() {
      this.$axios.get(this.$store.state.url+"/studentByName?name=" + this.search_name).then((res) => {
        if (res.status === 200) {
          this.tableData = res.data.results;
        }
      });
    }
  },
};
</script>

<style scoped>
.biaoge {
  margin-top: 30px;
}
.block {
  float: right !important;
}
</style>

<style>
.user .el-input__inner {
  width: 200px !important;
}
.el-pagination .el-select .el-input .el-input__inner {
  padding-right: 25px;
  border-radius: 3px;
  width: 100px !important;
}
.el-pagination__editor.el-input .el-input__inner {
  width: 40px !important;
}
</style>
